/**
 * 资产模块Mock数据
 */
import Mock from 'mockjs'

// 创建随机数据生成器
const Random = Mock.Random

// 资产类别数据
const assetCategories = [
  { id: '1', name: '电脑设备', icon: 'laptop' },
  { id: '2', name: '办公设备', icon: 'print' },
  { id: '3', name: '通讯设备', icon: 'mobile-alt' },
  { id: '4', name: '医疗设备', icon: 'medkit' },
  { id: '5', name: '运输设备', icon: 'car' }
]

// 资产状态数据
const assetStatuses = [
  { id: 'idle', name: '闲置' },
  { id: 'in_use', name: '使用中' },
  { id: 'repair', name: '维修中' },
  { id: 'scrap', name: '报废' }
]

// 生成资产数据列表
const generateAssetList = (count = 10) => {
  const assets = []
  
  for (let i = 0; i < count; i++) {
    const categoryIndex = Random.integer(0, assetCategories.length - 1)
    const statusIndex = Random.integer(0, assetStatuses.length - 1)
    
    assets.push({
      id: Random.guid(),
      name: Random.pick([
        'Dell XPS笔记本电脑',
        'ThinkPad X1笔记本',
        'MacBook Pro',
        'iPhone 13 Pro',
        'iPad Pro',
        'HP LaserJet打印机',
        'Canon扫描仪',
        '会议室投影仪',
        '华为折叠屏手机',
        '超声波检测仪'
      ]),
      category: assetCategories[categoryIndex],
      code: 'A' + Random.integer(1000, 9999),
      status: assetStatuses[statusIndex],
      purchaseDate: Random.date('yyyy-MM-dd'),
      price: Random.float(1000, 20000, 2, 2),
      department: Random.pick(['行政部', '财务部', '人力资源部', '技术部', '销售部', '医疗部']),
      user: Random.cname(),
      location: Random.pick(['总部大楼', '分支机构', '仓库', '办公区', '会议室']),
      specs: Random.paragraph(1),
      manufacturer: Random.pick(['戴尔', '联想', '苹果', '华为', '惠普', '佳能']),
      maintainRecords: Random.integer(0, 5)
    })
  }
  
  return assets
}

// 模拟资产详情
const generateAssetDetail = (id) => {
  const categoryIndex = Random.integer(0, assetCategories.length - 1)
  const statusIndex = Random.integer(0, assetStatuses.length - 1)
  
  return {
    id,
    name: Random.pick([
      'Dell XPS笔记本电脑',
      'ThinkPad X1笔记本',
      'MacBook Pro',
      'iPhone 13 Pro',
      'iPad Pro',
      'HP LaserJet打印机',
      'Canon扫描仪',
      '会议室投影仪',
      '华为折叠屏手机',
      '超声波检测仪'
    ]),
    category: assetCategories[categoryIndex],
    code: 'A' + Random.integer(1000, 9999),
    status: assetStatuses[statusIndex],
    purchaseDate: Random.date('yyyy-MM-dd'),
    price: Random.float(1000, 20000, 2, 2),
    department: Random.pick(['行政部', '财务部', '人力资源部', '技术部', '销售部', '医疗部']),
    user: Random.cname(),
    location: Random.pick(['总部大楼', '分支机构', '仓库', '办公区', '会议室']),
    specs: Random.paragraph(1),
    manufacturer: Random.pick(['戴尔', '联想', '苹果', '华为', '惠普', '佳能']),
    serialNumber: 'SN' + Random.string('upper', 10),
    purchaser: Random.cname(),
    warrantyPeriod: Random.date('yyyy-MM-dd'),
    notes: Random.paragraph(),
    attachments: Array(Random.integer(0, 3)).fill().map(() => ({
      id: Random.guid(),
      name: `附件${Random.integer(1, 100)}.${Random.pick(['pdf', 'doc', 'jpg', 'png'])}`,
      url: Random.url(),
      size: Random.integer(100, 10000) + 'KB'
    })),
    maintainRecords: Array(Random.integer(0, 3)).fill().map(() => ({
      id: Random.guid(),
      date: Random.date('yyyy-MM-dd'),
      type: Random.pick(['保养', '维修', '升级']),
      description: Random.sentence(),
      cost: Random.float(100, 5000, 2, 2),
      operator: Random.cname()
    })),
    transferRecords: Array(Random.integer(0, 3)).fill().map(() => ({
      id: Random.guid(),
      date: Random.date('yyyy-MM-dd'),
      fromDepartment: Random.pick(['行政部', '财务部', '人力资源部', '技术部', '销售部', '医疗部']),
      toDepartment: Random.pick(['行政部', '财务部', '人力资源部', '技术部', '销售部', '医疗部']),
      fromUser: Random.cname(),
      toUser: Random.cname(),
      reason: Random.sentence(),
      approver: Random.cname()
    }))
  }
}

// 模拟API
export function setupAssetMock() {
  // 获取资产列表
  Mock.mock(/\/asset\/list/, 'get', (options) => {
    const { url } = options
    // 解析查询参数
    const params = new URLSearchParams(url.split('?')[1])
    const pageSize = parseInt(params.get('pageSize') || '10')
    const currentPage = parseInt(params.get('currentPage') || '1')
    const category = params.get('category')
    const status = params.get('status')
    const keyword = params.get('keyword')
    
    // 生成资产数据
    let data = generateAssetList(50)
    
    // 根据查询条件筛选
    if (category) {
      data = data.filter(item => item.category.id === category)
    }
    if (status) {
      data = data.filter(item => item.status.id === status)
    }
    if (keyword) {
      data = data.filter(item => 
        item.name.includes(keyword) || 
        item.code.includes(keyword) ||
        item.user.includes(keyword)
      )
    }
    
    // 分页处理
    const total = data.length
    const start = (currentPage - 1) * pageSize
    const end = start + pageSize
    const pageData = data.slice(start, end)
    
    return {
      code: 200,
      message: '操作成功',
      data: {
        list: pageData,
        pagination: {
          total,
          pageSize,
          currentPage
        }
      }
    }
  })
  
  // 获取资产详情
  Mock.mock(/\/asset\/detail\/\w+/, 'get', (options) => {
    const id = options.url.split('/').pop()
    return {
      code: 200,
      message: '操作成功',
      data: generateAssetDetail(id)
    }
  })
  
  // 获取资产类别
  Mock.mock('/asset/categories', 'get', () => {
    return {
      code: 200,
      message: '操作成功',
      data: assetCategories
    }
  })
  
  // 申请资产领用
  Mock.mock('/asset/apply', 'post', (options) => {
    return {
      code: 200,
      message: '申请提交成功',
      data: {
        id: Random.guid(),
        applyTime: Random.datetime(),
        status: '待审批'
      }
    }
  })
  
  // 申请资产维修
  Mock.mock('/asset/repair', 'post', (options) => {
    return {
      code: 200,
      message: '维修申请提交成功',
      data: {
        id: Random.guid(),
        applyTime: Random.datetime(),
        status: '待审批'
      }
    }
  })
  
  // 更新资产状态
  Mock.mock(/\/asset\/status\/\w+/, 'put', (options) => {
    return {
      code: 200,
      message: '状态更新成功',
      data: null
    }
  })
}

export default {
  assetCategories,
  assetStatuses,
  generateAssetList,
  generateAssetDetail,
  setupAssetMock
} 